<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Common extends Controller
{

	public function _initialize()
	{
		$rule = strtolower(request()->module()."/".request()->controller()."/".request()->action());


		if(session('admin')&&session('admin')->admin_id==1){
			return;
		}

		if(!session('admin')){
			$this->error('你还没登录 ，请先登录');
		}

		if(!$this->checkAuth($rule,session('admin')->admin_id)){
			$this->error('你没有权限访问');
		}
	}

	public function checkAuth($rule,$admin_id)
	{
		// var_dump($rule,$admin_id);

		$roles =  Db::name('auth_admin_role')
		->field('*')
		->alias('t1')
		->join('auth_role t2','t1.role_id = t2.role_id')
		->where('status','0')
        ->where('t1.admin_id',$admin_id)
		->select();


		$rules = [];

		foreach ($roles as $role) {
			$rules = array_merge(explode(',',$role['rules']),$rules);
		}

		$rules = array_unique($rules);

		$rules = Db::name('auth_rule')->where('rule_id','in',$rules)->column('rule_url');

		if(!in_array($rule,$rules)){
			return false;
		}

		return true;
	}
	
}